package com.zhipu.balance.utils.upload.corn

import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.listener.ChooseClickCallback
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.mobile.event.RecordCorn
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.remotedata.InterceptObserver
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.dialog.CommonDialog
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.Call
import okhttp3.MediaType

/**
 * 玉米上传
 */
object CornUploadUtils {

    /**
     * 外网上传玉米天平数据
     */
    fun uploadCornBalanceInfo(mRecordCorn : RecordCorn, mChooseClickCallback : ChooseClickCallback?){
        var mNowMills = TimeUtils.getNowMills()
        var mGson = GsonUtils.toJson("${mRecordCorn}&${mNowMills}")
        var byteSign = InitializeUtils.getSignV0()?.signatureParams(mGson)
        var signature = ConvertUtils.bytes2HexString(byteSign)
        var mObservable = InitializeUtils.AppComponent()?.systemService?.uploadCornBalanceInfo(signature,mNowMills,mRecordCorn)
        mObservable?.subscribeOn(Schedulers.io())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : InterceptObserver<String>() {
            /**
             * 成功数据data
             */
            override fun <T> onNextData(resultEntity: ResultEntity<T>?) {
                InitializeUtils.getDefaultMaker()?.show(resultEntity?.msg)
                mChooseClickCallback?.onChooseClickCallback(0)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

            /**
             * 错误网络
             */
            override fun onErrorNet(t: Throwable?, netMsg: String?) {
                InitializeUtils.getDefaultMaker()?.show("上传失败")
                mChooseClickCallback?.onChooseClickCallback(1)
                /**
                 * 关闭
                 */
                CommonDialog.dismissGP()
            }

        })
    }


    /**
     * 内网上传玉米天平数据
     */
    fun IntranetCornBalanceInfo(mRecordCorn : RecordCorn, mChooseClickCallback : ChooseClickCallback?){
        OkHttpUtils.postString()
            .url("http://"+ SPUtils.getInstance().getString("NetworkSettings","https://cg.zhipu-china.com/") + "/uploadCornBalanceInfo")
            .content(GsonUtils.toJson(mRecordCorn))
            .mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(object : StringCallback() {
                override fun onError(call: Call?, e: Exception?, id: Int) {
                    InitializeUtils.getDefaultMaker()?.show("上传失败")
                    mChooseClickCallback?.onChooseClickCallback(1)
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
                override fun onResponse(response: String?, id: Int) {
                    var mResultEntity = GsonUtils.fromJson(response,ResultEntity::class.java)
                    when(mResultEntity.status){
                        200 ->{
                            InitializeUtils.getDefaultMaker()?.show(mResultEntity.msg)
                            mChooseClickCallback?.onChooseClickCallback(0)
                        }
                        else ->{
                            InitializeUtils.getDefaultMaker()?.show("上传失败")
                            mChooseClickCallback?.onChooseClickCallback(1)
                        }
                    }
                    /**
                     * 关闭
                     */
                    CommonDialog.dismissGP()
                }
            })
    }
}